/**
 * 设置cookie
 * @param name cookie的名称
 * @param value cookie的值
 * @param day cookie的过期时间
 */
var setCookie = function (name, value, day) {
    if (day !== 0) {     //当设置的时间等于0时，不设置expires属性，cookie在浏览器关闭后删除
        var expires = day * 24 * 60 * 60 * 1000;
        var date = new Date(+new Date() + expires);
        document.cookie = name + "=" + escape(value) + ";expires=" + date.toUTCString();
    } else {
        document.cookie = name + "=" + escape(value);
    }
};

/**
 * 获取对应名称的cookie
 * @param name cookie的名称
 * @returns {null} 不存在时，返回null
 */
var getCookie = function (name) {
    var arr;
    var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
};

/**
 * 删除cookie
 * @param name cookie的名称
 */
var delCookie = function (name) {
    setCookie(name, ' ', -1);
};


var params = {}
if (document) {
    params.domain = document.domain || ''; //拿到域名
    params.url = document.URL || '';    //拿到url地址
    params.title = document.title || ''; //拿到网站标题
    params.referrer = document.referrer || ''; //当前页面和之前页面的链接
}
var xiaojiuid = getCookie('xiaojiuid')
var is_new;
if (xiaojiuid) {
    is_new = false
} else {
    xiaojiuid = Math.floor(1099511627776 * Math.random());
    setCookie('xiaojiuid', xiaojiuid, 1)
    is_new = true
}
// http://localhost:3000/api/statistics/pv  https://api.lzwzw.cn/api/statistics/pv

let options = {
    method: 'post',//post请求
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({//post请求参数
        domain: params.domain,
        is_new,
    })
}
fetch('https://api.lzwzw.cn/api/statistics/pv', options).then(res => {
    return res.json()
}).then(res => {
    document.getElementById('xiaojiu_container_site_pv').style.display = ''
    document.getElementById('xiaojiu_value_site_pv').innerText = res.site_pv
    document.getElementById('xiaojiu_container_site_uv').style.display = ''
    document.getElementById('xiaojiu_value_site_uv').innerText = res.site_uv
})
    .catch(console.error)